//
//  BuyTypeViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/17.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//

#import "BillInfoViewController.h"

@interface BillInfoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger type;
@property (nonatomic, strong) UITextField *textField;

@end

@implementation BillInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWhite;
    [self loadBaseTitleLable:@"发票信息"];
    self.type = 0;
    
    [self.view addSubview:self.tableView];
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    NSArray *array = @[@"不开发票",@"个人",@"企业"];
    cell.textLabel.textColor = UIColorFromRGB(0x323232);
    cell.textLabel.font = UIFontSystem(15);
    cell.textLabel.text = array[indexPath.row];
    
    if (self.type == indexPath.row) {
        cell.contentView.backgroundColor = tableViewBgColor;
    }
    cell.selectionStyle = NO;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return rateHeight(100);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.type == 0) {
        return rateHeight(150);
    } else {
        return rateHeight(250);
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (self.type == 0) {
        UIView *bgV = [[UIView alloc] init];
        bgV.backgroundColor = kClearColor;
        
        UIButton *btn = [UIButton buttonWithTitle:@"确认并保存" fontSize:15 titleColor:UIColorWhite background:navBarColor cornerRadius:4];
        [bgV addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_offset(CGSizeMake(rateWidth(600), rateHeight(80)));
            make.center.equalTo(bgV);
        }];
        
        return bgV;
    } else {
        UIView *bgV = [[UIView alloc] init];
        bgV.backgroundColor = kClearColor;
        
        UITextField *textField = [[UITextField alloc] init];
        textField.textColor = UIColorFromRGB(0x323232);
        textField.font = UIFontSystem(14);
        if (self.type == 1) {
            textField.text = @"个人";
        } else {
            textField.placeholder = @"发票抬头 例如：公司全称";
        }
        textField.layer.masksToBounds = YES;
        textField.layer.borderColor = globalGray.CGColor;
        textField.layer.borderWidth = 1;
        textField.layer.cornerRadius = 4;
        [bgV addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(bgV);
            make.top.equalTo(bgV).offset(rateHeight(40));
            make.size.mas_offset(CGSizeMake(rateWidth(600), rateHeight(70)));
        }];
        self.textField = textField;
        
        UIButton *btn = [UIButton buttonWithTitle:@"确认并保存" fontSize:15 titleColor:UIColorWhite background:navBarColor cornerRadius:4];
        [bgV addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(bgV);
            make.size.mas_offset(CGSizeMake(rateWidth(600), rateHeight(80)));
            make.centerX.equalTo(bgV);
        }];
        [btn addTarget:self action:@selector(saveAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        return bgV;
    }
}
- (void)saveAction
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.type == 0) {
        // 不开发票
        [dic setValue:@"不开发票" forKey:@"text"];
        [self.delegate sendBillDic:dic];
        [self.navigationController popViewControllerAnimated:YES];
    } else if (self.type == 1) {
        // 个人
        [dic setValue:@"个人" forKey:@"text"];
        if (_textField.text.length != 0) {
            [dic setValue:_textField.text forKey:@"remark"];
            [self.delegate sendBillDic:dic];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [TFHUD addRemindHUD:self.view message:@"请填写发票抬头"];
        }
        
    } else {
        // 企业
        [dic setValue:@"企业" forKey:@"text"];
        if (_textField.text.length != 0) {
            [dic setValue:_textField.text forKey:@"remark"];
            [self.delegate sendBillDic:dic];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [TFHUD addRemindHUD:self.view message:@"请填写发票抬头"];
        }
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.type = indexPath.row;
    [self.tableView reloadData];
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeight-SafeAreaTopHeight) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = UIColorWhite;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        //        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
